<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 控制器基类
 * @property UserORM   $user	     当前登录的用户对象
 * @property ApiOutput $apiOutput API输出类
 * @property CI_DB_query_builder $db
 */
class MY_Controller extends CI_Controller{
	/**
	 * 获取当前登录的用户
	 * @param	bool 	$isExit 当用户不存在时,是否退出脚本并输出错误信息(默认TRUE)
	 * @param	string	$msg	用户未登录时的错误消息(默认:登录已失效,请重新登陆)
	 * @return UserORM 用户对象
	 */
	public function getLoginUser($isExit=TRUE,$msg=''){
		static $user = NULL;
		if ( NULL === $user ) {
			$this->load->model('User_mdl');
			$user = User_mdl::getLoginUser();
			if ( $isExit ) {
				if ( empty($user) ){
					$msg = empty($msg) ? '登录已失效,请重新登陆' : $msg;
					$this->apiOutput->error($msg,$this->apiOutput->eUnAuth);
				}else if( ! $user->checkLastLoginPhoneID() ){
					$this->apiOutput->error('您的账户已在其他手机登录,请重新登陆',$this->apiOutput->eUnAuth);
				}
			}
		}
		return $user;
	}
	/**
	 *
	 * @param UserORM $user
	 */
	protected function checkBindPhone($user){
		if( ! $user->isBindPhone() ){
			$user->logout();
			$this->apiOutput->error('登录已失效,请重新登陆',$this->apiOutput->eUnAuth);
		}
	}

}
/**
 * @deprecated	用户_控制器_基类
 * @abstract 进入该控制器需要用户已登陆
 *
 * @property User_mdl  $users	用户表模型
 * @property UserORM   $user	 当前用户对象
 * @property ApiOutput $apiOutput API输出类
 * @property Order_mdl $order 订单模型类
 */
// class User_Controller extends CI_Controller{
// 	public function __construct(){
// 		parent::__construct();
// 		$this->load->model('User_mdl','users');
// 		$user = $this->users->getUser();
// 		if (empty($user)) {
// 			$this->apiOutput->error('登录已失效,请重新登陆',$this->apiOutput->eUnAuth);
// 		}
// 		if( $user->isTest==1 ){

//         }else if( ! $user->checkLastLoginPhoneID() ){
// 			$this->apiOutput->error('您的账户已在其他手机登录,请重新登陆',$this->apiOutput->eUnAuth);
// 		}
// 		$this->user = $user;
// 	}
// }
/**
 * 后台进程-控制器-基类
 * @property CI_DB_query_builder $db
 * @property Order_mdl $order
 */
class Bg_Controller extends CI_Controller{
	public function __construct(){
		parent::__construct();
		if( ! defined('BG_PROCESS') ){
// 			exit('无权限进入');
		}
	}
}

/**
 * 后台_控制器_基类
 * @property CI_Router $router
 * @property CI_Loader $load
 * @property Admin_mdl $admin
 * @property Search_tpl $searchTpl
 * @property CI_Input $input
 */
class Admin_Controller extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->model('Admin_mdl','admin');
		$admin = $this->admin->getUser() ;
		// 判断是否已登陆
		if( empty($admin) ){
			redirect(ADMIN_DIR.'login/login');
			exit;
		}
		// 权限验证
		$ctrl   = $this->router->fetch_class();
		$method = $this->router->fetch_method();
		$link   = $ctrl.'/'.$method;

		// 不用进行权限验证的链接
		if( $ctrl == 'main' || $link == 'manager/modify_psw' ){
			return TRUE;
		}
		// 其他链接进行权限验证
		if( !$this->admin->checkLink($admin['id'], $link) ){
			$msg = '您无此操作权限,请联系超级管理员为您开通';
			if( $this->input->is_ajax_request() ){
				$this->apiOutput->error($msg,$this->apiOutput->eUnAuth);
			}else{
				alert($msg,'back');
			}
			exit;
		}
	}
	protected function showView($tpl,$data=array(),$header=array()){
		if( empty($header) ){
			$header = $this->getViewHeaderData();
		}
		$headerTpl = 'admin/common/header';
		$footerTpl = 'admin/common/footer';
		$this->load->view($headerTpl,$header);
		$this->load->view($tpl,$data);
		$this->load->view($footerTpl);
	}
	/**
	 * 获取当前页面的头部信息
	 */
	protected function getViewHeaderData(){
		$method = $this->router->fetch_method();
		$class = $this->router->fetch_class();
		$link  = $class.'/'.$method;

		$this->load->model('Admin_menu_mdl','menu');
		$allMenuList = $this->menu->getAllMenuList();
		$allMenuList = array_combine( array_column($allMenuList, 'id') , $allMenuList);

		$parentID = 0;
		$title    = '后台管理';
		foreach ($allMenuList as $v){
			if( $v['link'] == $link ){
				$parentID = $v['parentID'];
				$title    = $v['title'];
				break;
			}
		}
		$placeList = array();//存储视图中"位置"数据
		while ( $parentID !=0 ) {
			if( isset($placeList,$allMenuList[$parentID]) ){
				array_unshift($placeList,$allMenuList[$parentID]);//将上级插入到栏目列表顶部
				$parentID = $allMenuList[$parentID]['parentID'];
			}else{
				$parentID=0;
			}
		}
		array_push($placeList, array('title'=>$title));
		$header = array();
		$header['title'] = $title;
		$header['placeList'] = $placeList;
		return $header;
	}

	protected function showHeader(){
		$this->load->view('admin/common/header',$this->getViewHeaderData());
	}
	/**
	 * 输出搜索表单
	 * @param array $items
	 * 普通输入框:
	 * 		array('type'=>'text','label'=>'测试','name'=>'test')  ==> <label>测试</label><input type="text" name="test" class="scinput1"/>
	 * 日期范围:
	  		array(
	 					'type' => 'dateRange',
	 					'label' => '充值时间',
						'begin' => array('name'=>'rc_st','value'=>date('Y-m-d 00:00:00',time()-86400*30)),
						'end'   => array('name'=>'rc_et','value'=>date('Y-m-d 23:59:59')),
				)
	 */
	protected function showSearchForm($items,$temp=array()){
		foreach ($items as $item){
			if ( is_array($item) ){

			}else{
				switch ($item) {
					case 'school':
						$this->load->model('School_mdl','school');
						$user=$_SESSION['admin'];
						if( empty($user) ){
							alert('未登录！',ADMIN_DIR.'login/login');
						}
						$school_id=isset($user['school_id'])?$user['school_id']:0;

						$where_array=array();
						if($school_id!=0){
							$school_id=explode(',',$school_id);
							$where_array=$school_id;
						}
						$this->load->model ( 'admin_mdl', 'adminmdl' );
						$is_admin=$this->adminmdl->isSuperManager($user['id']);

						if($is_admin){
							$temp['schoolList'] = $this->school->getSchoolList();
						}else{
							$temp['schoolList'] = $this->school->getSchoolList(array(),$where_array);
						}


					break;
				}
			}
		}
		$temp['formItems'] = $items;
		$this->load->view ( 'admin/searchTpl/searchForm', $temp );
	}

	protected function showTable($title,$thItems,$tdList,$totalRows=0){
		$temp = array(
				'tableTitle'=>$title,
				'thList'=>$thItems,
				'tdList'=>$tdList,
		);
		$this->load->view ( 'admin/searchTpl/table', $temp );
	}

	protected function showPagins($totalRows,$page=1,$pagesize=20,$paginCount=10){

	}

	protected function showStat($temp=array()){
		$this->load->view('admin/searchTpl/stat',$temp);
	}

	protected function showFooter(){
		$this->load->view('admin/searchTpl/footer');
	}

// 	protected function showSearchTpl($formItems,$tableTitle,$thItems,$list){
// 		$headerTpl = 'admin/common/header';
// 		$formTpl   = 'admin/searchTpl/form';
// 		$tableTpl  = 'admin/searchTpl/table';
// 		$pageTpl   = 'admin/searchTpl/page';
// 		$footerTpl = 'admin/common/footer';
// 		// ==== 输出头部
// 		$header = $this->getViewHeaderData();
// 		$this->load->view($headerTpl,$header);
// 		// ==== 输出表单
// 		$this->load->view($formTpl,array('formItems'=>$formItems));
// 		// ==== 输出表格
// 		$temp = array(
// 				'tableTitle'=>$tableTitle,
// 				'thList'=>$thItems,
// 				'tdList'=>$list,
// 		);
// 		$this->load->view($tableTpl,$temp);
// 		// ==== 输出分页数据
// 		$this->load->view($pageTpl,$temp);
// 		// ==== 输出脚部
// 		$this->load->view($footerTpl);
// 	}

// 	protected function query($query){

// 	}




}
